<?php
/**
 * User: hjd-yao
 * Date: 2019/6/12
 * Time: 9:37
 */

namespace DC\BonusCenter;


use App\Models\Bonus\BonusCountModel;
use App\Models\Bonus\BonusRecordModel;
use App\Models\PlatFinanceModel;
use App\Models\User\AccountFlowModel;
use Carbon\Carbon;
use DC\UserCenter\Models\UserWalletModel;
use Illuminate\Support\Facades\DB;
use Frontend\Models\UserBaseModel;

class CommonBonus
{


    public  $group = 0;
    public $settleScene = 1;

    protected $changeType = 0;
    protected $relateUid = 0;
    protected $relateInfo = [];
    protected $allot = [];

    protected $money2_rate ;
    protected $money3_rate ;
    protected $money4_rate ;
    protected $plat_rate ;

    public static function make()
    {
        return new static();
    }

    public function __construct(){

//        $this->money3_rate = get_rule('money3_rate') ?? 0 ;
//        $this->money4_rate = get_rule('charitable_rate') ?? 0 ;
//        $this->plat_rate = get_rule('plat_rate') ?? 0 ;
//        $this->money2_rate = 100 - $this->money3_rate - $this->money4_rate - $this->plat_rate ;
        $this->money2_rate = 100;

    }

    /**
     * @title 处理奖金期数
     * @param $group
     * @return $this
     */
    public function setGroup($group)
    {
        $this->group = $group;
        return $this;
    }


    /**
     * @param array $allots
     * @return $this
     */
    public function setBonusAllot($allots = [])
    {
        if(empty($allots)){
            $this->allot = [
                ['field'=>'money2','rates'=>$this->money2_rate,'type'=>1],
//                ['field'=>'money3','rates'=>$this->money3_rate,'type'=>1],
//                ['field'=>'other_fee','rates'=>$this->money4_rate,'type'=>0],
//                ['field'=>'plat_fee','rates'=>$this->plat_rate,'type'=>0]
            ];
        }else{
            $this->allot = $allots;
        }
        return $this;
    }

    /**
     * @param $change_type
     * @param $target_uid
     * @param $relate_uid
     * @param $bonus_money
     * @param $money_type
     * @return mixed
     * @throws \HttpException
     */
    public function settleBonus($change_type,$target_uid,$relate_uid,$bonus_money,$money_type)
    {
        $allots = $this->allot;
        if(empty($allots)){
            throw new \HttpException('缺少分配方案',403);
        }
        $bonus_rates = [];
        $other_rates = [];
        foreach ($allots as $allot){
            if($allot['type']==1){
                $bonus_rates[$allot['field']] =$allot['rates'];
            }else{
                $other_rates[$allot['field']] =$allot['rates'];
            }
        }
        $bonus_rate = array_sum($bonus_rates);
        $plat_rate = array_sum($other_rates);

        $fact_money = $bonus_money*$bonus_rate*0.01;
        $plat_fee = $bonus_money*$plat_rate*0.01;
        foreach ($bonus_rates as $field=>$currency_rates){
            $money =$bonus_money*$currency_rates*0.01;
            if($money){
                $before_money = UserWalletModel::where('member_id',$target_uid)->value($field);
                UserWalletModel::where('member_id',$target_uid)->increment($field,$money);
                AccountFlowModel::init()->createFlow($target_uid,$money,1,[
                    'before_money'=>$before_money,
                    'change_type'=>$change_type,
                    'relate_uid'=>$relate_uid,
                    'money_type'=>get_currency('name','id')[$field],
                ]);
            }
        }

        $other_fee = 0 ;
        foreach ($other_rates as $field_key=>$field_value){
            if($field_key =='plat_fee'){
                $plat_fee =  $bonus_money*$field_value*0.01;
            }else{
                $other_fee = $bonus_money*$field_value*0.01;
            }
        }

        $this->changeType = $change_type;
        $this->bonusRecord($target_uid,$relate_uid,$bonus_money,$fact_money,json_encode($bonus_rates),$plat_fee,$other_fee,json_encode($other_rates));//生成奖金记录
        PlatFinanceModel::init()->expendFinance($bonus_money);
        return $bonus_money;
    }


// 判断 是否出局

    public function settleOutBonus($change_type,$target_uid,$relate_uid,$money_type)
    {


        $allots = $this->allot;
        if(empty($allots)){
            throw new \HttpException('缺少分配方案',403);
        }

        $is_out = UserBaseModel::init()->where('member_id',$target_uid)->value('is_out');
        if($is_out){
            return false ;
        }

        $this_money = UserWalletModel::where('member_id',$target_uid)->value('money1');

        if($this_money >= get_rule('gw_award2')){
            UserBaseModel::init()->where('member_id',$target_uid)->upadte(['is_out'=>1]);
        }else{
            return false ;
        }

        $bonus_money = get_rule('gw_award3');

        $bonus_rates = [];
        $other_rates = [];
        foreach ($allots as $allot){
            if($allot['type']==1){
                $bonus_rates[$allot['field']] =$allot['rates'];
            }else{
                $other_rates[$allot['field']] =$allot['rates'];
            }
        }
        $bonus_rate = array_sum($bonus_rates);
        $plat_rate = array_sum($other_rates);

        $fact_money = $bonus_money*$bonus_rate*0.01;
        $plat_fee = $bonus_money*$plat_rate*0.01;
        foreach ($bonus_rates as $field=>$currency_rates){
            $money =$bonus_money*$currency_rates*0.01;
            if($money){
                $before_money = UserWalletModel::where('member_id',$target_uid)->value($field);
                UserWalletModel::where('member_id',$target_uid)->increment($field,$money);
                AccountFlowModel::init()->createFlow($target_uid,$money,1,[
                    'before_money'=>$before_money,
                    'change_type'=>$change_type,
                    'relate_uid'=>$relate_uid,
                    'money_type'=>$money_type,
                ]);
            }
        }
        $this->changeType = $change_type;
        $this->bonusRecord($target_uid,$relate_uid,$bonus_money,$fact_money,json_encode($bonus_rates),$plat_fee,json_encode($other_rates));//生成奖金记录

        return $bonus_money;
    }



    /**
     * @title生成奖金激励
     * @param $target_uid
     * @param $relate_uid
     * @param $bonus
     * @param $fact_bonus
     * @param null $rates
     * @param int $tax
     * @param int $other_fee
     * @param null $plat_rates
     */
    public function bonusRecord($target_uid,$relate_uid,$bonus,$fact_bonus,$rates=null,$tax=0,$other_fee=0,$plat_rates= null)
    {

        $data['target_uid']=$target_uid;
        $data['relate_uid']=$relate_uid;
        $data['bonus_type']=$this->changeType;
        $data['bonus']=$bonus;
        $data['fact_bonus']=$fact_bonus;
        $data['rates']=$rates;
        $data['tax']=$tax;
        $data['other_fee']=$other_fee;
        $data['plat_rates']=$plat_rates;
        $data['group']=$this->group;
        $data['settle_scene']=$this->settleScene;
        BonusRecordModel::create($data);
        $this->bonusCount($target_uid,$bonus,$fact_bonus);
    }

    public function bonusCount($target_uid,$bonus,$fact_bonus)
    {
        $count_date=Carbon::today()->timestamp;
        $data=array(
            'target_uid'=>$target_uid,
            'date'=>$count_date,
            'created_at'=>$count_date
        );
        $where['target_uid'] = $target_uid;
        $where['date'] = $count_date;

        $data['bonus']=DB::raw('IFNULL(`bonus`,0)+'.$bonus);
        $data['fact_bonus']=DB::raw('IFNULL(`fact_bonus`,0)+'.$fact_bonus);
        $field = 'bonus'.$this->changeType;
        $fact_field = 'fact_bonus'.$this->changeType;
        $data[$field]=DB::raw('IFNULL(`'.$field.'`,0)+'.$bonus);
        $data[$fact_field]=DB::raw('IFNULL(`'.$fact_field.'`,0)+'.$fact_bonus);

        return BonusCountModel::updateOrInsert($where,$data);
    }


}